package thread;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileCopy extends JFrame implements ActionListener{
    private JTextField tf1,tf2;//文本框
    private JProgressBar pb;//进度条
    private JButton bt1,bt2,cp;
    private File srcFile=null;
    private String desPath=null;
    public FileCopy(){
        setTitle("文件复制");
        setSize(600,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);
        JLabel label1=new JLabel("选择文件");//设置标签
        JLabel label2=new JLabel("复制到");
        label1.setBounds(50,50,60,25);
        label2.setBounds(50,100,60,25);
        add(label1);add(label2);
        tf1=new JTextField();
        tf2=new JTextField();
        tf1.setBounds(110,50,300,25);
        tf2.setBounds(110,100,300,25);
        tf1.setEditable(false);//禁止编辑
        tf2.setEditable(false);
        add(tf1);add(tf2);
        bt1=new JButton("..");
        bt2=new JButton("..");
        bt1.setBounds(420,50,60,25);
        bt2.setBounds(420,100,60,25);
        bt1.addActionListener(this);//设置动作监听，点击可弹出文件目录
        bt2.addActionListener(this);
        add(bt1);add(bt2);
        cp=new JButton("复制");
        cp.setBounds(230,250,60,25);
        cp.addActionListener(this);
        add(cp);
        pb=new JProgressBar();//进度条
        pb.setStringPainted(true);//显示百分号
        pb.setBounds(70,175,400,25);
        add(pb);
    }

    @Override
    public void actionPerformed(ActionEvent e) {//动作监听
        Object o=e.getSource();//被点击事件
        if (o==bt1){//选择文件
            JFileChooser fc=new JFileChooser();//选择新的文件
            //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int f=fc.showOpenDialog(this);//f为0代表打开了文件
            if (f==JFileChooser.APPROVE_OPTION){
               srcFile=fc.getSelectedFile();
               tf1.setText(srcFile.getPath());
            }
        }
        else if (o==bt2){//选择目的文件
            JFileChooser fc=new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能选择文件夹
            int f=fc.showOpenDialog(this);
            if (f==JFileChooser.APPROVE_OPTION){
                desPath=fc.getSelectedFile().getPath();
                tf2.setText(desPath);
            }

        }
        else if (o==cp){//开始复制
            if (srcFile==null||desPath==null){
                JOptionPane.showMessageDialog(this,"请选择文件");
                return;
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File desFile=new File(desPath+"\\"+srcFile.getName());
                    try {
                        FileInputStream fis=new FileInputStream(srcFile);
                        FileOutputStream fos=new FileOutputStream(desFile);
                        byte[] tmp=new byte[9182];
                        int c=fis.read(tmp);
                        long fsize=srcFile.length();
                        int percent=0;
                        while (c!=-1){
                            fos.write(tmp,0,c);
                            percent=(int)((1.0-1.0*fis.available()/fsize)*100);
                            pb.setValue(percent);
                            c=fis.read(tmp);
                        }
                        fos.close();fis.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public static void main(String[] agrs){
         SwingUtilities.invokeLater(new Runnable() {

             @Override
             public void run() {
                 new FileCopy();
             }
         });
     }
}
